package com.wolfalpha.utron.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.umeng.analytics.MobclickAgent;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.ui.UIHelper;

import easemob.applib.utils.HXPreferenceUtils;
import easemob.chatuidemo.utils.PreferenceUtils;

/**
 * 消息设置列表界面
 *
 * @author lodon
 */
public class SettingMessageActivity extends BaseNormalActivity implements OnClickListener {

    private RelativeLayout rlMVibrate;      //震动
    private RelativeLayout rlMVoice;        //声音
    private RelativeLayout rlMQun;          //群消息
    private RelativeLayout rlMLight;        //指示灯
    private RelativeLayout rlMShowContent;  //通知显示内容
    private RelativeLayout rlMReciveNews;   //资讯推送

    //图片
    private ImageView ivMVibrate;
    private ImageView ivMVoice;
    private ImageView ivMQun;
    private ImageView ivMLight;
    private ImageView ivMShowContent;
    private ImageView ivMReciveNews;

    //状态
    private boolean b_vib = true;
    private boolean b_voi = true;
    private boolean b_qun = true;
    private boolean b_lig = true;
    private boolean b_con = true;
    private boolean b_new = true;

    private EMChatOptions chatOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_message);
        chatOptions = EMChatManager.getInstance().getChatOptions();


        initView();
        initListeners();

        loadSettings();
    }

    private void loadSettings() {
        b_voi = HXPreferenceUtils.getInstance().getSettingMsgSound();
        checkOO(ivMVoice, b_voi);
        System.out.println(b_voi);

        b_vib = HXPreferenceUtils.getInstance().getSettingMsgVibrate();
        checkOO(ivMVibrate, b_vib);
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setTvTitle("消息设置");

        rlMVibrate = (RelativeLayout) findViewById(R.id.rl_setting_message_vibrate);
        ivMVibrate = (ImageView) findViewById(R.id.iv_message_vibrate);
        rlMVoice = (RelativeLayout) findViewById(R.id.rl_setting_message_voice);
        ivMVoice = (ImageView) findViewById(R.id.iv_message_voice);
        rlMQun = (RelativeLayout) findViewById(R.id.rl_setting_message_toast_qun);
        ivMQun = (ImageView) findViewById(R.id.iv_message_qun);
        rlMLight = (RelativeLayout) findViewById(R.id.rl_setting_message_light);
        ivMLight = (ImageView) findViewById(R.id.iv_message_light);
        rlMShowContent = (RelativeLayout) findViewById(R.id.rl_setting_message_toast_content);
        ivMShowContent = (ImageView) findViewById(R.id.iv_message_show_content);
        rlMReciveNews = (RelativeLayout) findViewById(R.id.rl_setting_message_recive_news);
        ivMReciveNews = (ImageView) findViewById(R.id.iv_message_recive_news);
    }

    /**
     * 初始化监听
     */
    private void initListeners() {
        rlMVibrate.setOnClickListener(this);
        rlMVoice.setOnClickListener(this);
        rlMQun.setOnClickListener(this);
        rlMLight.setOnClickListener(this);
        rlMShowContent.setOnClickListener(this);
        rlMReciveNews.setOnClickListener(this);
    }


    /**
     * 切换图标
     *
     * @param imageView
     * @param on
     */
    private void checkOO(ImageView imageView, boolean on) {
        if (on) {
            imageView.setImageResource(R.mipmap.check_on_big);
        } else {
            imageView.setImageResource(R.mipmap.check_off_big);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_setting_message_vibrate:
                b_vib = !b_vib;
                checkOO(ivMVibrate, b_vib);
                break;
            case R.id.rl_setting_message_voice:
                b_voi = !b_voi;
                checkOO(ivMVoice, b_voi);
                break;
            case R.id.rl_setting_message_toast_qun:
                checkOO(ivMQun, b_qun);
                b_qun = !b_qun;
                break;
            case R.id.rl_setting_message_light:
                checkOO(ivMLight, b_lig);
                b_lig = !b_lig;
                break;
            case R.id.rl_setting_message_toast_content:
                checkOO(ivMShowContent, b_con);
                b_con = !b_con;
                break;
            case R.id.rl_setting_message_recive_news:
                checkOO(ivMReciveNews, b_new);
                b_new = !b_new;
                break;
        }

//        chatOptions.setNotificationEnable(false);
        chatOptions.setNoticedByVibrate(b_vib);
        chatOptions.setNoticeBySound(b_voi);
        EMChatManager.getInstance().setChatOptions(chatOptions);

        HXPreferenceUtils.getInstance().setSettingMsgSound(b_voi);
        HXPreferenceUtils.getInstance().setSettingMsgVibrate(b_vib);

    }

}
